import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisDemoHash {
    public static void test01(Jedis jedis) {
        System.out.println("hset 和 hget");
        jedis.flushAll();
        jedis.hset("key", "f1", "111");
        Map<String, String> map = new HashMap<>();
        map.put("f2", "222");
        map.put("f3", "333");
        jedis.hset("key", map);
        String result = jedis.hget("key", "f1");
        System.out.println("result：" + result);
        result = jedis.hget("key", "f2");
        System.out.println("result：" + result);
        result = jedis.hget("key", "f100");
        System.out.println("result：" + result);
    }

    public static void test02(Jedis jedis) {
        System.out.println("判断是否存在 hexists");
        jedis.flushAll();
        jedis.hset("key", "f1", "111");
        jedis.hset("key", "f2", "111");
        jedis.hset("key", "f3", "111");
        boolean result = jedis.hexists("key", "f1");
        System.out.println("result：" + result);

        result = jedis.hexists("key", "f100");
        System.out.println("result：" + result);
    }

    public static void test03(Jedis jedis) {
        System.out.println("删除操作 hdel");
        jedis.flushAll();
        jedis.hset("key", "f1", "111");
        jedis.hset("key", "f2", "111");
        jedis.hset("key", "f3", "111");
        long result = jedis.hdel("key", "f1", "f2");
        System.out.println("result：" + result);
        boolean hexists = jedis.hexists("key", "f1");
        System.out.println("hexists:" + hexists);
        hexists = jedis.hexists("key", "f2");
        System.out.println("hexists:" + hexists);
        hexists = jedis.hexists("key", "f3");
        System.out.println("hexists:" + hexists);
    }

    public static void test04(Jedis jedis) {
        System.out.println("hkeys 和 hvals");
        jedis.flushAll();
        jedis.hset("key", "f1", "111");
        jedis.hset("key", "f2", "111");
        jedis.hset("key", "f3", "111");

        Set<String> hkeys = jedis.hkeys("key");
        List<String> hvals = jedis.hvals("key");
        System.out.println("hvals:" + hkeys);
        System.out.println("hvals:" + hvals);
    }

    public static void test05(Jedis jedis) {
        System.out.println("hmget 和 hmset");
        jedis.flushAll();
        Map<String,String> map = new HashMap<>();
        map.put("f1","111");
        map.put("f2","222");
        map.put("f3","333");

        jedis.hmset("key",map);
        List<String> hmget = jedis.hmget("key", "f1", "f2", "f3");
        System.out.println("hmget:" + hmget);
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");

        try (Jedis jedis = jedisPool.getResource()) {
            test01(jedis);
//            test02(jedis);
//            test03(jedis);
//            test04(jedis);
//            test05(jedis);
        }
    }
}
